// pages/tixian/index.js
let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardlist: [],
    cardnumber: "请选择",
    userInfo: {},
    money: 0,

    truename: ""
  },
  choosecard(e) {
    var index = e.detail.value;
    console.log("index", index);
    var cardnumber = this.data.cardlist[index].cardnumber;
    var truename = this.data.cardlist[index].truename;
    console.log("cardnumber", truename);
    if (cardnumber == "使用新卡提现") {
      wx.navigateTo({
        url: "/pages/tixian/add"
      })
    } else {
      this.setData({
        cardnumber: cardnumber,
        truename: truename
      });
    }

  },
  moneyedit: function(e) {

    this.obj.money = e.detail.value
  },
  alltixian() {

    this.setData({
      money: this.data.userInfo.mymoney
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log("onshow");
    this.getcardlist();
    this.getuserinfo();
    this.tixianconfig();
  },
  getcardlist() {
    var that = this;
    App._get('Index/cardlist', {}, function(result) {
      console.log("result.data.list", result.data);
      var list = result.data;
      list.unshift({
        cardnumber: '使用新卡提现'
      })
      that.setData({
        cardlist: list
      });

    });
  },
  moneyedit: function(e) {
    this.data.money = e.detail.value
  },
  getuserinfo() {
    var that = this;
    App._get('user.index/detail', {}, result => {
      console.log("result.data", result.data);
      that.setData({
        userInfo: result.data.userInfo
      });
    });
  },
  tixianconfig() {
    var that = this;
    App._get('Index/tixianconfig', {}, result => {
      console.log("result.data", JSON.parse(result.data.values));
      that.setData({
        config: JSON.parse(result.data.values)
      });
    });
  },
  primary() {
    console.log("ss123");
    var money = this.data.money;
    var cardnumber = this.data.cardnumber;
    if (cardnumber == "请选择") {
      App.showError("请选择银行卡");
      return;
    }

    if (money == "" || money == 0) {
      App.showError("请输入提现金额");
      return;
    }
    if (money > this.data.userInfo.mymoney) {
      App.showError("提现金额不能大于余额");
      return;
    }
    if (money <= parseInt(this.data.config.maxprice) && money >= parseFloat(this.data.config.minprice)) {


      var truename = this.data.truename;

      App._post_form('Index/addwithdraw', {
        money: money,
        cardnumber: cardnumber,
        truename: truename
      }, function(result) {
        console.log("result.data.list", result);
        if (result.code == 1) {
          App.showError('提交成功', function() {
            console.log("ss");
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1000);
          });
          
        }


      });
    } else {
      App.showError("最低提现额度:" + this.data.config.minprice + ",最大提现额度:" + this.data.config.maxprice);
      return;
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})